<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/27
  Time: 14:45
  To change this template use File | Settings | File Templates.
--%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>个人信息</title>
</head>
<link rel="stylesheet" href="css/header.css">


<body background="resourse/bg.jpg">
<%--判断是否登录--%>
<%
    String userid=(String)session.getAttribute("userid");
//    System.out.print("Session userID="+userid);

    if (userid!=null) {
%>

<div id="header"><!--页头-->
    <spana >&emsp;巴以小记</spana>
    <br>
    <spanb >&emsp;&emsp;<%=(String)session.getAttribute("userid")%> 在线中..</spanb>
    <ul>
        <li></li>
        <li><h2 class="main" ><a class="btna" href="main.jsp">主页</a></h2></li>
        <li><h2 class="work"><a class="btna" href="addInfo.jsp">发贴</a></h2></li>
        <li><h2 class="new"><a class="btna" href="showInfo.jsp" >看贴</a></h2></li>
        <li><h2 class="comment"><a class="btna" href="showInfo.jsp" >搜索</a></h2></li>
    </ul>
</div>

    <%
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/talknewsdatabase";
        String user = "root";
        String password = "123456";
        conn = DriverManager.getConnection(url,user,password);
    //    stat = conn.createStatement();
    //    rs = stat.executeQuery("select * from stu");
    %>


<br>
    <form action="delete_checkbox.jsp">
    <table  cellSpacing=3 >
        <tr>
            <th width="10px">
                <input type="submit" value ="×">
            </th>
            <th width="150px">编号</th>
            <th width="200px">类型</th>
            <th>标题</th>
            <th width="100px">内容</th>
            <th width="80px"></th>
            <th width="80px"></th>
        </tr>
        <%
            //分页展示
            String pageN=request.getParameter("pageN");
            int pageSize=8; //单页8条
            int lineCount;
            int pageCount;
            int pageNow=1;
            if(pageN!=null){
                pageNow=Integer.parseInt(pageN);
            }

            stat = conn.prepareStatement("select count(*) from info where user_id=" + userid   );
            rs = stat.executeQuery();
            rs.next();
            lineCount = rs.getInt(1);
            pageCount= lineCount%pageSize==0 ? lineCount/pageSize : lineCount/pageSize +1;
            if(pageNow<=0){
                pageNow=1;
            }
            if(pageNow>=pageCount){
                pageNow=pageCount;
            }

//            while(rs.next()) {
//                System.out.println(rs.getString("user_id"));//测试
//           }

            System.out.println("lineCount=" + lineCount);
            stat = conn.prepareStatement("select * from info where user_id='" + userid+  "' limit ?,?");
//            stat = conn.prepareStatement("select * from info limit ?,?");
            stat.setInt(1,pageSize*(pageNow-1));
            stat.setInt(2,pageSize);
            rs = stat.executeQuery();


            while(rs.next()){   //输出表格内容
                Integer deleteid = rs.getInt("id");
                out.print("<tr>");
                out.println("<td>" + "<input type=\"checkbox\" name = \"delete_id\" value=\"" + deleteid + "\">" + "</td>");
        %>
                <td><%=rs.getString("id")%></td>
                <td><%=rs.getString("name")%></td>
                <td><%=rs.getString("title")%></td>
                <td  align="center" ><a href="select_for_id.jsp?id=<%=rs.getString("id")%>">查看内容</a></td>

                <td  align="center" width="40px"><a href="delete.jsp?id=<%=rs.getString("id") %>">删除</a></td>
                <td  align="center" width="40px"><a href="update.jsp?id=<%=rs.getString("id") %>">修改</a></td>
                </tr>
        <%
            }
        %>
    </table>

        <%--首上下尾搜索法--%>
        <div>
            [<a href="showInfo.jsp?pageN=1"> 首页 </a>]
            [<a href="showInfo.jsp?pageN=<%=pageNow-1%>"> 上一页 </a>]
            [<a href="showInfo.jsp?pageN=<%=pageNow+1%>"> 下一页 </a>]
            [<a href="showInfo.jsp?pageN=<%=pageCount%>"> 尾页 </a>]
            <br>
        </div>
        <br>

        <%--精准搜索--%>
        <div>
            <select onchange="location.replace('showInfo.jsp?pageN='+this.value);">
                <%for(int i = 1; i<=pageCount;i++){%>
                    <%if(pageNow==i){%>
                        <option value="<%=i%>" selected="selected"> <%=i%> </option>
                    <%}else{%>
                        <option value="<%=i%>"> <%=i%> </option>
                    <%}%>
                <% } %>
            </select>
            总共<%=pageCount%>页，当前在第<%=pageNow%>页
        </div>
    </form>



<%--查询区--%>

    <%--    <form action="select_for_id.jsp" method="post">--%>
    <%--        <h3> ID号搜索:<input type="text" name="id"  value="" title="ID号不能为空"> </input>--%>
    <%--            <input type="submit" value="搜索"/></h3>--%>
    <%--    </form>--%>

    <form action="select_for_name.jsp" method="post">
        <h3> 类型搜索:<input type="text" name="name" value="" title="类型不能为空"></input>
            <input type="submit" value="搜索" /></h3>
    </form>

    <%--<form action="select_for_pick.jsp" method="post">--%>
    <%--    <h3> 关注度搜索:<input type="text" name="age" value="" title="关注度不能为空"></input>--%>
    <%--        <input type="submit" value="搜索"/></h3>--%>
    <%--</form>--%>

    <form action="select_for_title.jsp" method="post">
        <h3> 标题搜索:<input type="text" name="title" value="" title="标题不能为空"></input>
            <input type="submit" value="搜索"/></h3>
    </form>

    <form action="select_for_comment.jsp" method="post">
        <h3> 内容搜索:<input type="text" name="comment" value="" title="内容不能为空"></input>
            <input type="submit" value="搜索"/></h3>
    </form>


    <%
        if(rs != null) {
            rs.close();
            rs = null;
        }
        if(stat != null) {
            stat.close();
            stat = null;
        }
        if(conn != null) {
            conn.close();
            conn = null;
        }
    %>

<%
    } else {
        out.print("<script>alert('未成功登录！');" +
                "window.location='http://localhost:8080/index.html';</script>");
    }
%>

</body>
</html>
